package com.learn.springcloud.eurekahaconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @Time : 2018/7/23 19:40
 * @Version : 1.0
 * @Author : hongyan.li@hand-china.com
 * Copyright @ 2000 Shanghai Hand Co. Ltd.
 * All right reserved.
 */
@RestController
public class GetConsumer {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * getForEntity(String url, Class responseType, Object... urlVariables)
     * @param name
     * @return
     */
    @GetMapping(value = "/consumer/getForEntity/example1/{name}")
    public String getForEntityExample1(@PathVariable String name){
        String url = "http://ribbon-resttemplate-example-provider/provider/getForEntity/service?name={1}";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, name);
        return responseEntity.getBody();
    }


    /**
     * getForEntity(String url, Class responseType, Map urlVariables)
     * @param name
     * @return
     */
    @GetMapping(value = "/consumer/getForEntity/example2/{name}")
    public String getForEntityExample2(@PathVariable String name){
        String url = "http://ribbon-resttemplate-example-provider/provider/getForEntity/service?name={name}";
        Map<String, String> urlVariables = new HashMap<>();
        urlVariables.put("name", name);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, urlVariables);
        return responseEntity.getBody();
    }


    /**
     * getForEntity(URI uri, Class responseType)
     * @param name
     * @return
     */
    @GetMapping(value = "/consumer/getForEntity/example3/{name}")
    public String getForEntityExample3(@PathVariable String name){
        UriComponents uriComponents = UriComponentsBuilder
                .fromUriString("http://ribbon-resttemplate-example-provider/provider/getForEntity/service?name={name}")
                .build()
                .expand(name)
                .encode();
        URI uri = uriComponents.toUri();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri,String.class);
        return responseEntity.getBody();
    }


    /**
     * getForObject(String url, Class<T> responseType, Object... uriVariables)
     * @param name
     * @return
     */
    @GetMapping(value = "/consumer/getForObject/example1/{name}")
    public String getForObjectExample1(@PathVariable String name){
        String url = "http://ribbon-resttemplate-example-provider/provider/getForObject/service?name={name}";
        String result = restTemplate.getForObject(url,String.class,name);
        return result;
    }


    /**
     * getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
     * @param name
     * @return
     */
    @GetMapping(value = "/consumer/getForObject/example2/{name}")
    public String getForObjectExample2(@PathVariable String name){
        String url = "http://ribbon-resttemplate-example-provider/provider/getForObject/service?name={name}";
        Map<String,String> urlVariables = new HashMap<>();
        urlVariables.put("name",name);
        String result = restTemplate.getForObject(url,String.class,urlVariables);
        return result;
    }


    /**
     * getForObject(URI url, Class<T> responseType)
     * @param name
     * @return
     */
    @GetMapping(value = "/consumer/getForObject/example3/{name}")
    public String getForObjectExample3(@PathVariable String name){
        UriComponents uriComponents = UriComponentsBuilder
                .fromUriString("http://ribbon-resttemplate-example-provider/provider/getForObject/service?name={name}")
                .build()
                .expand(name)
                .encode();
        URI uri = uriComponents.toUri();
        String result = restTemplate.getForObject(uri,String.class);
        return result;
    }

}
